<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//error_reporting(E_ALL);
//ini_set('display_errors', TRUE);
//ini_set('display_startup_errors', TRUE);


$user_id = get_loggedin_userid();
//$user_id = get_input('user_id');
$interest_id = get_input('interest_id');

/*

F = Feedback identifier
<verb> = like or dislike

Prerequisite: <number> {including your opinion} is greater than 1
F1: <number> people <verb> <interest name>.
Example: 1,234 people like Elliot Lee.
Format string: %@ people %@ %@.

Prerequisite: at least 2 friends <verb> this
F2: <friend 1 first name>, <friend 2 first name>, and <number> others <verb> this.
Example: Thuy, Pho, and 2,345 others like this.
Format string: %@, %@, and %@ others %@ this.
Note: <number> is the number of Opinions users who <verb> this; NOT just friends

Prerequisite: <percent> is less than 40
F3: Only <percent>% <verb> this.
Example: Only 9% dislike this.
Format string: Only %u%% dislike this.

Prerequisite: at least 3 friends <verb> this
F4: <friend first name> and 23 others <verb> this.
Example: Mark and 1,234 others like this.
Format string: %@ and %@ others like this.

Prerequisite: you are the first to <verb> <interest name>
F5: You are the first to <verb> <interest name>.
Example: You are the first to like Aquafina water.
Format string: You are the first to %@ %@.

When determining which feedback to show:
Choose randomly among the ones whose prerequisite is met
Store feedback in a flexible manner (in a database or array) so that we can easily add more feedback in the future
Validate feedback in a flexible manner

 */

function genRString($choices, $number, $percent, $friends, $verb, $interest_name) {
//    echo "<pre>";
//    echo "<br>chocies<br>";
//    print_r($choices);
//    echo "<br>number = $number<br>";
//    echo "<br>percent = $percent<br>";
//    echo "<br>friends<br>";
//    print_r($friends);
//    echo "<br>verb = $verb<br>";
//    echo "<br>interest_name = $interest_name<br>";
//    echo "";
//    echo "</pre>";
    if(count($choices) <= 0) {
        return "You $verb $interest_name.";
    }
    $choice = $choices[rand(0, count($choices) - 1)];
    switch ($choice) {
        case 1:
            if($number == 1) {
            	if ($verb == 'feel so so about') {
        			$verb = 'feels so so about';
        		} else {
        			$verb .= 's';
        		}
            	return "Only 1 person $verb $interest_name.";
			} else {
				return "$number people $verb $interest_name.";
			}
        case 2:
            $friend_id_1 = $friends[0]->user_id;
            $friend1 = get_user($friend_id_1);
            $friend_name_1 = $friend1->name;
            $friend_id_2 = $friends[1]->user_id;
            $friend2 = get_user($friend_id_2);
            $friend_name_2 = $friend2->name;
            $other_number = $number - 2;
            return "$friend_name_1, $friend_name_2 and $other_number others $verb this.";
        case 3:
            return "Only $percent% $verb this.";
        case 4:
            $friend_id_1 = $friends[0]->user_id;
            $friend1 = get_user($friend_id_1);
            $friend_name_1 = $friend1->name;
            $other_number = $number - 1;
            return "$friend_name_1 and $other_number others $verb this.";
        case 5:
            return "You are the first to $verb $interest_name.";
        default:
            return "You $verb $interest_name.";
    }
}

function genString($choice, $number, $percent, $friends, $verb, $interest_name) {
//    echo "<pre>";
//    echo "<br>chocies<br>";
//    print_r($choice);
//    echo "<br>number = $number<br>";
//    echo "<br>percent = $percent<br>";
//    echo "<br>friends<br>";
//    print_r($friends);
//    echo "<br>verb = $verb<br>";
//    echo "<br>interest_name = $interest_name<br>";
//    echo "";
//    echo "</pre>";
    switch ($choice) {
        case 1:
        	if($number == 1) {
        		if ($verb == 'feel so so about') {
        			$verb = 'feels so so about';
        		} else {
        			$verb .= 's';
        		}
            	return "Only 1 person $verb $interest_name.";
			} else {
				return "$number people $verb $interest_name.";
			}
        case 2:
            $friend_id_1 = $friends[0]->user_id;
            $friend1 = get_user($friend_id_1);
            $friend_name_1 = $friend1->name;
            $friend_id_2 = $friends[1]->user_id;
            $friend2 = get_user($friend_id_2);
            $friend_name_2 = $friend2->name;
            $other_number = $number - 2;
            return "$friend_name_1, $friend_name_2 and $other_number others $verb this.";
        case 3:
            return "Only $percent% $verb this.";
        case 4:
            $friend_id_1 = $friends[0]->user_id;
            $friend1 = get_user($friend_id_1);
            $friend_name_1 = $friend1->name;
            $other_number = $number - 1;
            return "$friend_name_1 and $other_number others $verb this.";
        case 5:
            return "You are the first to $verb $interest_name.";
        default:
            return "You $verb $interest_name.";
    }

}

function validateP1($choices, $number) {
    if($number > 1) {
//        echo "<br>pass P1<br>";
        $choices[] = 1;
    }
    return $choices;
}

function validateP2($choices, $friends) {
    if(count($friends) > 1) {
//        echo "<br>pass P2<br>";
        $choices[] = 2;
    }
    return $choices;
}

function validateP3($choices, $percent) {
    if($percent < 40) {
//        echo "<br>pass P3<br>";
        $choices[] = 3;
    }
    return $choices;
}

function validateP4($choices, $friends) {
    if(count($friends) > 2) {
//        echo "<br>pass P4<br>";
        $choices[] = 4;
    }
    return $choices;
}

function validateP5($choices, $number) {
    if($number == 1) {
//        echo "<br>pass P5<br>";
        $choices[] = 5;
    }
    return $choices;
}

$interest = get_interest($interest_id);

$like_choices = array();
$dislike_choices = array();
$other_choices = array();

if(!$interest) {
    $like_string = "You are the first one to like this.";
    $dislike_string = "You are the first one to dislike this.";
    $other_string = "You are the first one to not care about this.";
    $strings = array(
        "-1"    => $other_string,
        "0"     => $dislike_string,
        "2"     => $like_string
    );
    die(json_encode(export_result(400, $strings)));
}
$user_count = count_user();
$rating_count = intval(count_interest_ratings_on($interest_id));
$like_choices = validateP5($like_choices, $rating_count);
$dislike_choices = validateP5($dislike_choices, $rating_count);
$other_choices = validateP5($other_choices, $rating_count);

    
$like_count = intval(count_interest_ratings_with_rating_on($interest_id, 2)) + 1;
$like_choices = validateP1($like_choices, $like_count);
//$like_percent = intval($like_count * 1.0 / $user_count);
$like_percent = number_format($like_count * 1.0 / $user_count, 2);
$like_choices = validateP3($like_choices, $like_percent);
$like_friends = get_friends_with_rating_of_user($user_id, $interest_id, 2, 3, 0);
$like_choices = validateP2($like_choices, $like_friends);
$like_choices = validateP4($like_choices, $like_friends);

$dislike_count = intval(count_interest_ratings_with_rating_on($interest_id, 0)) + 1;
$dislike_choices = validateP1($dislike_choices, $dislike_count);
//$dislike_percent = intval($dislike_count * 1.0 / $user_count);
$dislike_percent = number_format($dislike_count * 1.0 / $user_count, 2);
$dislike_choices = validateP3($dislike_choices, $dislike_percent);
$dislike_friends = get_friends_with_rating_of_user($user_id, $interest_id, 0, 3, 0);
$dislike_choices = validateP2($dislike_choices, $dislike_friends);
$dislike_choices = validateP4($dislike_choices, $dislike_friends);

$other_count = intval(count_interest_ratings_with_rating_on($interest_id, -1)) + 1;
$other_choices = validateP1($other_choices, $other_count);
//$other_percent = intval($other_count * 1.0 / $user_count);
$other_percent = number_format($other_count * 1.0 / $user_count, 2);
$other_choices = validateP3($other_choices, $other_percent);
$other_friends = get_friends_with_rating_of_user($user_id, $interest_id, -1, 3, 0);
$other_choices = validateP2($other_choices, $other_friends);
$other_choices = validateP4($other_choices, $other_friends);

//$strings = array(
//    "-1"    => genRString($other_choices, $rating_count, $other_percent, $other_friends, "feel so so about", $interest->name),
//    "0"     => genRString($dislike_choices, $rating_count, $dislike_percent, $dislike_friends, "dislike", $interest->name),
//    "2"     => genRString($like_choices, $rating_count, $like_percent, $like_friends, "like", $interest->name)
//);

$like_strings = array();
foreach($like_choices as $like_choice) {
    $like_strings[] = genString($like_choice, $rating_count, $like_percent, $like_friends, "like", $interest->name);
}

$dislike_strings = array();
foreach($dislike_choices as $dislike_choice) {
    $dislike_strings[] = genString($dislike_choice, $rating_count, $dislike_percent, $dislike_friends, "dislike", $interest->name);
}

$other_strings = array();
foreach($other_choices as $other_choice) {
    $other_strings[] = genString($other_choice, $rating_count, $other_percent, $other_friends, "feel so so about", $interest->name);
}

$strings = array(
    "-1"    => $other_strings,
    "0"     => $dislike_strings,
    "2"     => $like_strings
);

echo json_encode(export_result(200, $strings));
?>
